﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.Permission.Data
{
    /// <summary>
    /// 数据访问类FunctionData。
    /// </summary>
    public class FunctionData
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "FunctionId", "FunctionName", "FunctionAlias", "FunctionFlag", "FunctionDes" };
        private string tableName = "HF_Function";
        private string keyField = "FunctionId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(FunctionEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@FunctionId", entity.FunctionId, typeof(string));
            sqlDataItem.AppendParameter("@FunctionName", entity.FunctionName, typeof(string));
            sqlDataItem.AppendParameter("@FunctionAlias", entity.FunctionAlias, typeof(string));
            sqlDataItem.AppendParameter("@FunctionFlag", entity.FunctionFlag, typeof(bool));
            sqlDataItem.AppendParameter("@FunctionDes", entity.FunctionDes, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public FunctionEntity GetFunctionEntity(DataRow dr)
        {
            FunctionEntity entity = new FunctionEntity();
            string value = "";
            entity.FunctionId = dr["FunctionId"].ToString();
            entity.FunctionName = dr["FunctionName"].ToString();
            entity.FunctionAlias = dr["FunctionAlias"].ToString();
            value = dr["FunctionFlag"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.FunctionFlag = true;
                }
                else
                {
                    entity.FunctionFlag = false;
                }
            }
            entity.FunctionDes = dr["FunctionDes"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public FunctionData()
        { }
        public static FunctionData GetInstance()
        {
            return new FunctionData();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(FunctionEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(FunctionEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string functionId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, functionId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="functionId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string functionId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, functionId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="FunctionId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetFunctionTable(string functionId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, functionId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="functionId">主键Id</param>
        /// <returns>DataTable</returns>
        public FunctionEntity GetFunctionEntity(string functionId)
        {
            DataTable dt = GetFunctionTable(functionId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetFunctionEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        /// 获取所有功能
        /// </summary>
        /// <returns>DataTable</returns>
        public  DataTable GetAllFunctions()
        {
            try
            {
                string tmpSql = "select * from HF_Function order by FunctionId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获取所有功能
        /// </summary>
        /// <returns>Entitys</returns>
        public List<FunctionEntity> GetAllFunctionEntitys()
        {
            try
            {
                List<FunctionEntity> result = new List<FunctionEntity>();
                DataTable dt = GetAllFunctions();
                foreach (DataRow dr in dt.Rows)
                {
                    FunctionEntity entity = GetFunctionEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获取模块下的所有功能
        /// </summary>
        /// <param name="moduleId">模块Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetFunctionsByModule(string moduleId)
        {
            try
            {
                string tmpSql = "select * from HF_Function where left(FunctionId,2)=@moduleId order by FunctionId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.AppendParameter("@moduleId", moduleId);
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取模块下的所有功能
        /// </summary>
        /// <param name="moduleId">模块Id</param>
        /// <returns>Entitys</returns>
        public List<FunctionEntity> GetFunctionEntitysByModule(string moduleId)
        {
            try
            {
                List<FunctionEntity> result = new List<FunctionEntity>();
                DataTable dt = GetFunctionsByModule(moduleId);
                foreach (DataRow dr in dt.Rows)
                {
                    FunctionEntity entity = GetFunctionEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取具有访问权限的功能
        /// </summary>
        /// <param name="moduleId">模块Id</param>
        /// /// <param name="groupId">权限组Id</param>
        /// <returns>功能DataTable</returns>
        public  DataTable GetPowerFunctions(string moduleId, string groupId)
        {
            try
            {
                string tmpSql = "select distinct f.FunctionId,f.FunctionName,f.FunctionDes,f.FunctionFlag,f.FunctionAlias from HF_Purview " +
                                                   " p left join HF_Function f on left(p.operationId,4)=f.FunctionId " +
                                                   " where left(f.FunctionId,2)=@moduleId and p.groupId=@groupId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.AppendParameter("@moduleId", moduleId);
                sqlItem.AppendParameter("@groupId", groupId);
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获得模块下最大的功能编号，取最后两位
        /// </summary>
        /// <param name="moduleId">2位模块编号</param>
        /// <returns>2位功能编号</returns>
        public string GetMaxFunctionId(string moduleId)
        {
            string tmpSql = "select right(functionid,2) functionId from hf_function where left(functionid,2)=@moduleId order by functionid desc ";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.AppendParameter("@moduleId", moduleId);
            sqlItem.CommandText = tmpSql;
            ClientDBAgent agent = new ClientDBAgent();
            string MaxId = agent.ExecuteScalar(sqlItem);
            if (string.IsNullOrEmpty(MaxId))
            {
                MaxId = "00";
            }
            return MaxId;
        }
        /// <summary>
        /// 获取具有访问权限的功能
        /// </summary>
        /// <param name="moduleId">模块Id</param>
        /// /// <param name="groupId">权限组Id</param>
        /// <returns>功能Entitys</returns>
        public List<FunctionEntity> GetPowerFunctionEntitys(string moduleId, string groupId)
        {
            try
            {
                List<FunctionEntity> result = new List<FunctionEntity>();
                DataTable dt = GetPowerFunctions(moduleId,groupId);
                foreach (DataRow dr in dt.Rows)
                {
                    FunctionEntity entity = GetFunctionEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 功能下是否包含操作
        /// </summary>
        /// <param name="moduleId">功能Id</param>
        /// <returns>是否</returns>
        public bool HasOperation(string functionId)
        {
            try
            {
                string tmpSql = "select * from HF_Operation where  " +
                               "  left(OperationId, 4) = @functionId ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.AppendParameter("@functionId", functionId);
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.RecordExists(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

